<?php 
namespace app\admin\controller;
use support\exception\ValidateException;
use app\admin\model\Crontab as CrontabModel;
use think\facade\Db;

class CrontabController extends CommonController {


	/*
 	* @Description  数据列表
 	*/
	function index(){
		if($this->request->method() == 'GET'){
			return view('crontab/index');
		}else{
			$limit  = $this->request->post('limit', 20);
			$page = $this->request->post('page', 1);

			$where = [];
			$where['crontab_id'] = $this->request->post('crontab_id');
			$where['name'] = $this->request->post('name');
			$where['type'] = $this->request->post('type');
			$where['status'] = $this->request->post('status');

			$field = 'crontab_id,name,type,rule,status,target,last_run_time';

			$order  = $this->request->post('order');	//排序字段
			$sort  = $this->request->post('sort');		//排序方式

			$orderby = ($sort && $order) ? $sort.' '.$order : 'crontab_id desc';

			$query = CrontabModel::field($field);

			$res = formatWhere($query,$where)->order($orderby)->paginate(['list_rows'=>$limit,'page'=>$page])->toArray();

			$data['status'] = 200;
			$data['data'] = $res;
			return json($data);
		}
	}


	/*
 	* @Description  修改排序开关
 	*/
	function updateExt(){
		$postField = 'crontab_id,status';
		$data = $this->request->only(explode(',',$postField),'post');
		if(!$data['crontab_id']) throw new ValidateException ('参数错误');
		CrontabModel::update($data);
		return json(['status'=>200,'msg'=>'操作成功']);
	}

	/*
 	* @Description  添加
 	*/
	public function add(){
		$postField = 'name,type,rule,status,target,remark';
		$data = $this->request->only(explode(',',$postField),'post');

		$validate = new \app\admin\validate\CrontabValidate;
		if(!$validate->scene('add')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		try{
			$res = CrontabModel::insertGetId($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'data'=>$res,'msg'=>'添加成功']);
	}


	/*
 	* @Description  修改
 	*/
	public function update(){
		$postField = 'crontab_id,name,type,rule,status,target,remark';
		$data = $this->request->only(explode(',',$postField),'post');

		$validate = new \app\admin\validate\CrontabValidate;
		if(!$validate->scene('update')->check($data)){
			throw new ValidateException ($validate->getError());
		}

		try{
			CrontabModel::update($data);
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'msg'=>'修改成功']);
	}


	/*
 	* @Description  修改信息之前查询信息的 勿要删除
 	*/
	function getUpdateInfo(){
		$id =  $this->request->post('crontab_id');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'crontab_id,name,type,rule,status,target,remark';
		$res = CrontabModel::field($field)->find($id);
		return json(['status'=>200,'data'=>$res]);
	}


	/*
 	* @Description  删除
 	*/
	function delete(){
		$idx =  $this->request->post('crontab_id');
		if(!$idx) throw new ValidateException ('参数错误');
		CrontabModel::destroy(['crontab_id'=>explode(',',$idx)],true);
		return json(['status'=>200,'msg'=>'操作成功']);
	}


	/*
 	* @Description  查看详情
 	*/
	function detail(){
		$id =  $this->request->post('crontab_id');
		if(!$id) throw new ValidateException ('参数错误');
		$field = 'crontab_id,name,type,rule,status,target,last_run_time';
		$res = CrontabModel::field($field)->find($id);
		return json(['status'=>200,'data'=>$res]);
	}




}

